package uranium.gui.add;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JTextField;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import uranium.db.DBManager;

import java.awt.Rectangle;
import javax.swing.JComboBox;

public class AddNewFattGUI extends JDialog{
		
	public void initialize(){
			//jFrame = new JFrame();
			this.setSize(new Dimension(412, 256));
			this.setTitle("Inserisci Fattura");
			this.setPreferredSize(new Dimension(250, 188));
			this.setContentPane(getJContentPane());
			centro();
			this.setVisible(true);
	}
	public AddNewFattGUI(DBManager dbm, JFrame jFrame){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		initialize();
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="218,125"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTextField jTextField = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JLabel jLabel2 = null;
	private JButton jButton1 = null;
	private DBManager dbm = null;
	private JButton jButton2 = null;
	private JLabel jLabelPagata = null;
	private JComboBox jComboBox = null;

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabelPagata = new JLabel();
			jLabelPagata.setBounds(new Rectangle(14, 89, 50, 16));
			jLabelPagata.setText("Pagata");
			jLabel2 = new JLabel();
			jLabel2.setText("Data (gg/mm/aaaa)");
			jLabel2.setBounds(new Rectangle(14, 66, 135, 16));
			jLabel1 = new JLabel();
			jLabel1.setText("Numero");
			jLabel1.setBounds(new Rectangle(14, 43, 77, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(250, 166));
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(jLabelPagata, null);
			jPanel.add(getJComboBox(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(200, 22));
			jTextField.setBounds(new Rectangle(156, 41, 200, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(200, 22));
			jTextField1.setBounds(new Rectangle(156, 64, 200, 22));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Inserisci");
			jButton1.setBounds(new Rectangle(210, 150, 83, 26));
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						inserisci();
					}
					catch(Exception e21){
						System.err.println(e21);
					}
					
				}
			});
		}
		return jButton1;
	}
	
	/**
	 * Il metodo inserisce il nuovo oggetto all'interno del db
	 * @throws SQLException 
	 */
	
	private synchronized void inserisci(){
		boolean error = false;
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(this, "Campo Numero Fattura non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField1.getText().trim().equals("")){
			JOptionPane.showMessageDialog(this, "Campo Data non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			String insert = null;
			String numero = jTextField.getText();
			String data = jTextField1.getText();
			String pagata = (String)jComboBox.getSelectedItem();
					
			insert="INSERT INTO fatture (numero,data,pagata) VALUES('"+numero+"','"+data+"','"+pagata+"')";	
			dbm.executeQuery(insert);
			this.dispose();
		}	
	}	
	
	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(98, 150, 83, 26));
			jButton2.setText("Annulla");
			jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e30){
						e30.printStackTrace();
					}
				}
			});
		}
		return jButton2;
	}
	
	private void close(){
		this.dispose();
	}
	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			Vector<String> v = new Vector<String>();
			v.add("SI");
			v.add("NO");
			jComboBox = new JComboBox(v);
			jComboBox.setBounds(new Rectangle(156, 87, 200, 22));
		}
		return jComboBox;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
